//
//  ZHTableView.swift
//  onemeetingmac
//
//  Created by 张衡 on 2020/3/17.
//  Copyright © 2020 onemeeting. All rights reserved.
//

import Foundation

@objc protocol ZHTableViewDelegate {
    
    @objc optional func zh_tableView(_ tableView: ZHTableView, didSelectRowAt indexPath: IndexPath)
    
    @objc optional func zh_tableView(_ tableView: ZHTableView, shouldSelectRow row: Int)->Bool
    
}

@objc protocol ZHTableViewDataSource {
    
    func zh_tableView(_ tableView: ZHTableView, numberOfRowsInSection section: Int)->Int
    
    func zh_tableView(_ tableView: ZHTableView, cellForRowAt indexPath: IndexPath)->NSView?
    
    @objc optional func zh_tableView(_ tableView: ZHTableView,tab:NSTableView, rowViewForRow row: Int)->NSTableRowView?
    
    @objc optional func zh_numberOfRows(in tableView: ZHTableView)->Int
    
    
    @objc optional func zh_tableView(_ tableView: ZHTableView, heightForRowAt indexPath: IndexPath)->CGFloat
    
    @objc optional func zh_tableView(_ tableView: ZHTableView, heightForHeaderInSection section: Int)->CGFloat
    
    
}

class ZHTableView: NSView {
    
    weak var dataSourse:ZHTableViewDataSource? {
        didSet {
            self.tabView.dataSource = self
        }
    }
    
    weak var delegate:ZHTableViewDelegate? {
        didSet {
            self.tabView.delegate = self
        }
    }
    
    var secion:Int = 1
    
    let separatorColor:NSColor = NSColor.clear
    
    var hasVerticalScroller:Bool = false {
        didSet {
            self.scrollView.hasVerticalScroller = hasVerticalScroller
        }
    }
    
    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        self.setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI()  {
        self.addSubview(scrollView)
        scrollView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
    }
    
    func reloadData() {
        self.tabView.kreloadData()
    }
    
    lazy var scrollView: NSScrollView = {
        let view = NSScrollView()
        view.hasVerticalScroller = false
        view.hasHorizontalScroller = false
        view.autohidesScrollers = true
        view.focusRingType = .none
        view.contentView = self.clipView
        view.backgroundColor = NSColor.white
        return view
    }()
    
    lazy var clipView: NSClipView = {
        let view = NSClipView.init(frame: NSRect.zero)
        view.focusRingType = .none
        view.documentView = self.tabView
        return view
    }()
    
    lazy var tabView: NSTableView = {
        let tab = NSTableView.init(frame: NSRect.zero)
        tab.wantsLayer = true
        tab.backgroundColor = color_dark
        tab.focusRingType = .none
        tab.allowsTypeSelect = false
        tab.selectionHighlightStyle = .none
        tab.gridColor = color_dark
        tab.gridStyleMask = NSTableView.GridLineStyle.solidVerticalGridLineMask
        
        let column = NSTableColumn.init(identifier: NSUserInterfaceItemIdentifier.init("column"))
        column.width = self.frame.width != 0 ? self.frame.width:200
        tab.addTableColumn(column)
        tab.headerView = nil
        
        return tab
    }()
    
}
extension ZHTableView: NSTableViewDelegate,NSTableViewDataSource {
    
    func numberOfRows(in tableView: NSTableView) -> Int {
        return self.dataSourse!.zh_tableView(self, numberOfRowsInSection: 0)
    }
    
    func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
        let index = IndexPath.init(item: row, section: 0)
        return self.dataSourse?.zh_tableView(self, cellForRowAt: index)
    }
    
    
    func tableView(_ tableView: NSTableView, rowViewForRow row: Int) -> NSTableRowView? {
        if self.dataSourse?.zh_tableView?(self, tab: tableView, rowViewForRow: row) == nil  {
            let tabRowView = NSTableRowView()
            let lineView = NSView()
            lineView.wantsLayer = true
            lineView.layer?.backgroundColor = self.separatorColor.cgColor
            tabRowView.addSubview(lineView)
            lineView.snp.makeConstraints { (make) in
                make.left.right.bottom.equalToSuperview()
                make.height.equalTo(0.6)
            }
            return tabRowView
        }else {
            return self.dataSourse?.zh_tableView?(self, tab: tableView, rowViewForRow: row)
        }
    }
    
    func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat {
        let indx = IndexPath.init(item: row, section: 0)
        if self.dataSourse?.zh_tableView?(self, heightForRowAt: indx) != nil {
            return (self.dataSourse!.zh_tableView?(self, heightForRowAt: indx))!
        }
        return 26
    }
    
    func tableView(_ tableView: NSTableView, shouldSelectRow row: Int) -> Bool {
        if self.delegate?.zh_tableView?(self, shouldSelectRow: row) != nil {
            return (self.delegate!.zh_tableView?(self, shouldSelectRow: row))!
        }
        return true
    }
    
    
    func tableViewSelectionDidChange(_ notification: Notification) {
        
        let tab = notification.object as? NSTableView
        let row = tab!.selectedRow
        
        //tab?.tableColumns
        let indx = IndexPath.init(item: row, section: 0)
        self.delegate?.zh_tableView?(self, didSelectRowAt: indx)
        //self.window!.performClose(nil)
        
    }
    
    
}

 


